#!/bin/bash

tmp="'"
TMPFILE="/tmp/mytmp"

#---------------------------check_load------------------------------
uptime=$(which uptime)
system_load=$($uptime | awk -F':' '{print $NF}' | sed 's/,//g')
one_load=$(echo $system_load| awk '{print $1}')
five_load=$(echo $system_load| awk '{print $2}')
fifteen_load=$(echo $system_load| awk '{print $1}')
echo '##check_load##=>##{"1min":"'$one_load'","5min":"'$five_load'","15min":"'$fifteen_load'"}##||' | sed "s/##/$tmp/g"

#-------------------------check_cupinfo-------------------------
cpu_type=$(cat /proc/cpuinfo | grep "model name" | awk -F[:@] '{print $2}' | sed 's/ \+$//g'| uniq)
cpu_mhz=$(cat /proc/cpuinfo | grep "model name" | awk -F[:@] '{print $3}' | uniq)
cpu_num=$(cat /proc/cpuinfo | grep "physical id" | sort | uniq -c | wc -l)
cpu_cores=$(cat /proc/cpuinfo | grep "cpu cores" |grep -o "[0-9]"|wc -l)
if [ -z $cpu_cores ];then
	cpu_cores=$($cat /proc/cpuinfo | grep "model name" | wc -l)
fi
echo '##check_cpuinfo##=>##{"cpu_type":"'"$cpu_type"'","cpu_mhz":"'"$cpu_mhz"'","cpu_number":"'"$cpu_num"'","cpu_cores":"'"$cpu_cores"'"}##||' | sed "s/##/$tmp/g"

#---------------------------check_disk---------------------------
file_system_type=`grep -vE "^#|swap|cdrom|^$" /etc/fstab |awk '{if ($2=="/") print $3}'` 
Filesystem=($(df -lht $file_system_type | sed  '/\/dev\/[^h,c,s,shm][^d]/N;s/\n//g'| awk 'NR>1{print $1}'))
Size=($(df -lht $file_system_type | sed  '/\/dev\/[^h,c,s,shm][^d]/N;s/\n//g'| awk 'NR>1{print $2}'))
Used=($(df -lht $file_system_type | sed  '/\/dev\/[^h,c,s,shm][^d]/N;s/\n//g'| awk 'NR>1{print $3}'))
Avail=($(df -lht $file_system_type | sed  '/\/dev\/[^h,c,s,shm][^d]/N;s/\n//g'| awk 'NR>1{print $4}'))
Use_per=($(df -lht $file_system_type | sed  '/\/dev\/[^h,c,s,shm][^d]/N;s/\n//g'| awk 'NR>1{print $5}'))
Mounted=($(df -lht $file_system_type | sed  '/\/dev\/[^h,c,s,shm][^d]/N;s/\n//g'| awk 'NR>1{print $6}'))

for ((i=0;i<${#Filesystem[@]};i++))
do
	if [ $i -lt 10 ]
	then
		NUM="0$i"
	else
		NUM=$i
	fi
echo '##check_disk_'$NUM'##=>##{"Filesystem":"'${Filesystem[$i]}'","Size":"'${Size[$i]}'","Used":"'${Used[$i]}'","Avail":"'${Avail[$i]}'","Use%":"'${Use_per[$i]}'","Mounted":"'${Mounted[$i]}'"}##||' | sed "s/##/$tmp/g"
done

#--------------------------check_meminfo----------------------------
mem_total=$(free -m | grep Mem | awk '{print $2}')
mem_free=$(free -m | grep Mem | awk '{print $4}')
mem_buff=$(free -m | grep "\-\/+" | awk '{print $3}')
mem_cache=$(free -m | grep "\-\/+" | awk '{print $4}')
mem_swap_total=$(free -m | grep Swap | awk '{print $2}')
mem_swap_free=$(free -m | grep Swap | awk '{print $4}')

echo '##check_meminfo##=>##{"mem_total":"'${mem_total}MB'","mem_free":"'${mem_free}MB'","mem_buffer":"'${mem_buff}MB'","mem_cache":"'${mem_cache}MB'","mem_swap_total":"'${mem_swap_total}MB'","mem_swap_free":"'${mem_swap_free}MB'"}##||' | sed "s/##/$tmp/g"

#---------------------------check_service---------------------------
str=`ss -4tlnp|awk 'NR>1 {sl=split($4,s,":");al=split($6,a,"\"");if (s[sl-1]!="127.0.0.1") print "\""s[sl]"_"a[al-1]"\":""\"""OK""\""","}'|sort|uniq`
str="'check_service'=>'{"$str"}'||"
echo $str|sed "s/,}'||/}'||/g;s/ //g"

#----------------------------get_10_of_cpu-----------------------------
ps -eo pid,%cpu,command  | grep -v PID | grep -v "0.0" | sort -nr -k 2  > $TMPFILE

PID=($(head -n 10 $TMPFILE|awk '{print $1}'))
used=($(head -n 10 $TMPFILE|awk '{print $2}'))
Cmd=($(head -n 10 $TMPFILE|awk '{print $3}'))

for (( i=0;i<${#PID[@]};i++ ))
do
	echo -e '##get_10_of_cpu_'$i'##=>##{"PID":"'${PID[$i]}'","%cpu":"'${used[$i]}'","command":"'${Cmd[$i]}'"}##||' | sed "s/##/$tmp/g"
done

#-----------------------------get_10_of_mem------------------------------
ps -eo pid,%mem,command  | grep -v PID | grep -v "0.0" | sort -nr -k 2  > $TMPFILE

PID=($(head -n 10 $TMPFILE|awk '{print $1}'))
used=($(head -n 10 $TMPFILE|awk '{print $2}'))
Cmd=($(head -n 10 $TMPFILE|awk '{print $3}'))

for (( i=0;i<${#PID[@]};i++ ))
do
	echo -e '##get_10_of_mem_'$i'##=>##{"PID":"'${PID[$i]}'","%mem":"'${used[$i]}'","command":"'${Cmd[$i]}'"}##||' | sed "s/##/$tmp/g"
done

#----------------------------check_bandwidth------------------------------
eth=($(ip -4 address |grep 'BROADCAST,MULTICAST,UP,LOWER_UP'|awk -F ': ' '{print $2}'))
for ((i=0;i<${#eth[@]};i++))
do
	RXpre=`expr $(/sbin/ip -s link show ${eth[$i]} | awk 'NR==4{print $1}')`
	TXpre=`expr $(/sbin/ip -s link show ${eth[$i]} | awk 'NR==6{print $1}')`

	if [ "$RXpre" -ne 0 ];then
		echo '##get_bandwidth_'$i'##=>##{"device":"'${eth[$i]}'","in":"'$RXpre'","out":"'$TXpre'"}##||' | sed "s/##/$tmp/g"
	fi
done

#-----------------------------get_onlineuser------------------------------
onlineuser=$(who |wc -l)
echo '##getonlineuser##=>##{"onlineuse":"'$onlineuser'"}##||' | sed "s/##/$tmp/g"

#get_uptime
onlinetime=$(uptime | awk '{print $3,$4,$5}'| sed 's/,$//g')
echo '##getonlinetime##=>##{"onlinetime":"'$onlinetime'"}##||' | sed "s/##/$tmp/g"

#-------------------------------all_pro------------------------------------
ps -ef|sed "s/[\']/\‘/g"|grep -v $$|egrep -v "(\[|/USR/SBIN/CRON|acpid|/sbin/rpc|resmgrd|irqbalance|nscd|xdm|zabbix_agentd|tty|cron|grep|ps|portmap|udevd|hald|klogd|sshd|dbus-daemon|xinetd|syslog|-bash|top\ |su\ -|-su|runall|get_10_of_)"|awk 'NR>1  {NUM=NR-2;if(NUM<10) NUM=(0""NUM);print "###all_pro_"NUM"###=>###|#|\"USER\":\""$1"\",\"PID\":\""$2"\",\"CMD\":\""$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16" "$17" "$18" "$19" "$20" "$21" "$22" "$23" "$24" "$25"\"|||###||" }'|sed "s/###/$tmp/g;s/|||/\}/g;s/|#|/\{/g"

